import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    mobileLogin: false,
    tabId:2,
    time:0,
    isChecked: false
  },

  handleChecked: function(e) {
    this.setData({
      isChecked: !this.data.isChecked
    })
  },

  navigateToUserWebview: function () {
    console.log('用户协议');
    wx.navigateTo({
      url: '/pages/login/webview?type=user',
    })
  },

  //账号登陆
  accountLogin: function(e) {
    if (!this.data.isChecked) return wx.showToast({ title: '请先查看并勾选下方协议按钮', icon: 'none' })
    var formdata = e.detail.value
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/sys/jwt/admin/login',
      method: 'POST',
      data: formdata
    }, (res) => {
      wx.hideLoading()
      wx.setStorageSync('Authorization', res.Authorization);
      this.getUserInfo();
      this.getAuthorizedItem();
      this.findPicLimt()
      let getUrl = wx.getStorageSync('redirectUrl');
      // wx.switchTab({
      //   url: '/pages/menber/index',
      // })
      if(getUrl) {
        // wx.removeStorage({
        //   key: 'redirectUrl',
        // })
        wx.navigateTo({
          url: '/pages/choose_colors/list?q=' + getUrl,
        })
      }else{
        wx.switchTab({
          url: '/pages/menber/index',
        })
      }
      
    })
  },


  onShow: function() {
    this.getchode()
  },

  //获取用户信息
  getUserInfo: function() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/user/getLoginUser',
      method: 'get',
    }, (res) => {
      wx.hideLoading()
      wx.setStorageSync('userInfo', res.vo);

    })
  },

  //获取当前登录用户的所有权限
  getAuthorizedItem: function() {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/structure/getAuthorizedItem',
      method: 'get',
    }, (res) => {
      wx.hideLoading()
      console.log(res)
      var list = res.list;
      var authorized = {}
      list.map(item => {
        var obj = {
          [item.authCode]: item.name
        }
        authorized = Object.assign(authorized, obj)
      })
      that.$globalSetData({
        permissions: authorized
      })
      wx.setStorageSync('authorized', authorized);

    })
  },


  getchode: function() {
    wx.login({
      success: (wxlogin) => {
        var that = this;
        this.setData({
          code: wxlogin.code
        })
      }
    })
  },



  //手机号码快捷登陆
  getPhoneNumber(opent) {
    if (!this.data.isChecked) return wx.showToast({ title: '请先查看并勾选下方协议按钮', icon: 'none' })
    var that = this;
    var data = {
      encryptedData: opent.detail.encryptedData,
      iv: opent.detail.iv,
      code: this.data.code,
      appId: app.globalData.appid,
    }
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/sys/jwt/admin/weixinLogin',
      contentType: "application/x-www-form-urlencoded",
      method: 'POST',
      data
    }, (res) => {
      console.log(res, 'res')
      wx.setStorageSync('Authorization', res);
      this.getUserInfo();
      this.getAuthorizedItem();
      this.findPicLimt()
      wx.switchTab({
        url: '/pages/menber/index',
      })
    })
    // wx.login({
    //   success: (wxlogin) => {
    //     var that = this;
    //     console.log(opent,'手机授权获取解密')
    //     console.log(wxlogin,'微信登陆信息')
    //     var data = {
    //       encryptedData: opent.detail.encryptedData,
    //       iv: opent.detail.iv,
    //       code: wxlogin.code,
    //       appId: app.globalData.appid,
    //     }
    //     _wxRequest.wxAjax({
    //       url: app.globalData.apiUrl + '/sys/jwt/admin/weixinLogin', 
    //       contentType: "application/x-www-form-urlencoded",
    //       method: 'POST',
    //       data
    //     }, (res)=>{
    //       console.log(res,'res')
    //       wx.setStorageSync('Authorization', res);
    //       this.getUserInfo();
    //       this.getAuthorizedItem();
    //       this.findPicLimt()
    //       wx.switchTab({
    //         url: '/pages/menber/index',
    //       })
    //     })

    //   }
    // }) 
  },


  findPicLimt: function() {
    //请求图片限制接口
    wx.request({
      url: app.globalData.apiUrl + '/sys/protected/comm/beanCrud/sys.DictVO/find',
      method: "post",
      header: {
        Authorization: wx.getStorageSync("Authorization")
      },
      success: (res) => {
        if (res.data.success) {
          var list = res.data.datas.list
          var obj = {}
          list.map(item => {
            var code = item.code.split('-')
            if (code && code.length > 3) {
              obj[code[4]] = item.val
            }
          })

          wx.setStorageSync('picLimit', obj);
        }
      }
    })
  },

 

  //微信登陆
  wxlogin: function() {
    if (!this.data.isChecked) return wx.showToast({ title: '请先查看并勾选下方协议按钮', icon: 'none' })
    var that = this;
      wx.login({
          success: (wxlogin) => {
            _wxRequest.wxAjax({
              url: app.globalData.apiUrl + '/sys/jwt/admin/wxLoginOnly',
              method: 'POST',
              contentType: 'application/x-www-form-urlencoded',
              data: {
                code: wxlogin.code,
                appId: app.globalData.appid
              },
            }, (res) => {
              console.log(res)

              if (res) {
                wx.setStorageSync('Authorization', res);
                that.getUserInfo();
                that.getAuthorizedItem();
                that.findPicLimt()
                wx.switchTab({
                  url: '/pages/menber/index',
                })
              } else {
                that.setData({
                  mobileLogin: true
                })
                wx.showToast({
                  title: '请选择手机号登陆',
                })
              }
            })
          }
      }) 
  },
 

  //tab切换
  handleTab: function (e) {
    var tabId = e.currentTarget.dataset.id
    this.setData({
      tabId
    }) 
  },


  // 获取验证码
  getCode() {
    if (this.data.time > 0) return;
    let { mobile } = this.data;
    if (!/^1\d{10}$/.test(mobile)) {
      wx.showToast({
        icon: "none",
        title: "手机号格式不正确"
      });
      return;
    } 
    _wxRequest.wxAjax(
      {
        url: app.globalData.apiUrl + "/sys/msg/sms/sendCode?mobile=" + mobile,
        method: "POST"
      },
      res => {
       this.countDown(120);
        wx.showToast({
          icon: "none",
          title: "验证码发送成功"
        });
      }
    );
  },

  // 倒计时
  countDownHandler: null,
  countDown(time) {
    this.setData({
      time
    });
    this.countDownHandler = setTimeout(() => {
      if (this.data.time > 0) {
        this.countDown(this.data.time - 1);
      } else {
        this.countDownHandler = null;
      }
    }, 1000);
  },

  inputPhone(e) {
    const mobile = e.detail.value;
    this.setData({
      mobile
    })
  },

  //短信登录
  mLogin: function (e) {
    if (!this.data.isChecked) return wx.showToast({ title: '请先查看并勾选下方协议按钮', icon: 'none' })
    var formdata = e.detail.value
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/sys/msg/sms/verifyCode',
      contentType: "application/x-www-form-urlencoded",
      method: 'POST',
      data: formdata
    }, (res) => {
      wx.hideLoading()
      wx.setStorageSync('Authorization', res.Authorization);
      this.getUserInfo();
      this.getAuthorizedItem();
      this.findPicLimt()
      wx.switchTab({
        url: '/pages/menber/index',
      })
    })
  },


})